import * as shipData from "@/Data/ship.json"
import { ICommodity } from "./commodity";

interface IShip {
    name: string;
    type: string;
    hp: number;
    mesh: shipMesh;
    entity: GameEntity;
    buildMoney: number;
    itemSize: number;
    speed: number;
    armor: number;
    country: string;
    weaItemSize: number;
    member: number;
    runCost: number;
    facingDirection: GameVector3
    view: number;
    mass: number
    detectionRange: number;
    weight: number;
    v: number
    structure: {
        left: number;
        right: number;
        front: number;
        back: number;
    };
    items: ShipItem[];
}

type shipMesh = {
    name?: string,
    mesh: GameModelAssets,
    scale: GameVector3 | { x: number, y: number, z: number },
}

export type shipCreatData = typeof shipData["圣特立尼达号"]

export class Ship implements IShip {
    items: ShipItem[];
    name: string;
    type: string;
    hp: number;
    mesh: shipMesh;
    buildMoney: number;
    itemSize: number;
    speed: number;
    armor: number;
    country: string;
    weaItemSize: number;
    member: number;
    runCost: number;
    view: number;
    entity: GameEntity;
    detectionRange: number;
    mass: number;
    facingDirection: GameVector3;
    weight: number;
    v: number;
    structure: { left: number; right: number; front: number; back: number; };
    constructor(data: shipCreatData) {
        this.name = data.name;
        this.buildMoney = data.buildMoney;
        this.itemSize = data.itemSize;
        this.speed = data.speed;
        this.armor = data.armor;
        this.country = data.country;
        this.weaItemSize = data.weaItemSize;
        this.member = data.member;
        this.runCost = data.runCost;
        this.view = data.view;
        this.detectionRange = data.detectionRange;
        this.weight = data.weight;
        this.structure = data.structure;
        this.type = data.type;
        this.hp = data.hp;
        this.mesh = data.mesh as shipMesh;
        this.mass = data.mass;
        this.items = [];
        this.facingDirection = new GameVector3(0, 0, 0)
        this.v = 0; // 初始化速度为 0，用于控制移动速度
        this.entity = world.createEntity({})!
        this.initializeModel()
        this.initializeData()
    }




    initializeModel() {
        this.entity.destroy();
        this.entity = world.createEntity(
            {
                mesh: this.mesh.mesh,
                meshScale: new GameVector3(this.mesh.scale.x, this.mesh.scale.y, this.mesh.scale.z),
                mass: this.mass,
                fixed: true
            })!;

        this.entity.showEntityName = true
        this.entity.customName = this.name
    }

    initializeData() {
        // 初始化数据的逻辑
        console.log(`Initializing data for ${this.name}`);
    }

    onInit() {

    }

    onMove() {

    }

    GiveItem(){

    }

    del(){
        this.entity.destroy();
    }


}

interface IPlayerShip {
    player: GamePlayerEntity
}

export class PlayerShip extends Ship implements IShip, IPlayerShip {
    player: GamePlayerEntity;
    constructor(data: shipCreatData, player: GamePlayerEntity) {
        super(data)

        this.player = player
        this.player.player.cameraEntity = this.entity
        console.log(this.entity.isPlayer)
        this.InitMove()
    }

    InitMove() {
        this.entity.lookAt(this.entity.position.add(this.player.player.facingDirection))
        this.facingDirection = this.player.player.facingDirection.clone()

        setTimeout(()=>{
            this.player.position = this.entity.position.add(new GameVector3(0,0.2,0))
        }, 500)
        this.player.player.onPress(async ({ button }) => {
            let isRun = true
            if (button == GameButtonType.RUN || button == GameButtonType.WALK) {
                const run = () => {
                    if(this.facingDirection.angle(this.player.player.facingDirection) >= 1.3) return
                    this.entity.lookAt(this.entity.position.add(this.player.player.facingDirection))
                    this.entity.velocity = this.player.player.facingDirection.scale(this.speed * this.v * 0.8)
                    this.facingDirection = this.player.player.facingDirection.clone()
                    sleep(1000 / 20).then(() => {
                        if (isRun) run()
                    })
                }

                run()

                this.player.player.onRelease(() => {
                    this.facingDirection = this.player.player.facingDirection.clone()
                    this.entity.velocity = this.player.player.facingDirection.scale(this.speed * this.v)
                    isRun = false
                })

            }

            if (button == GameButtonType.JUMP || button == GameButtonType.CROUCH) {
                const run = () => {
                    this.v += button == GameButtonType.JUMP ? 0.01 : -0.01
                    this.entity.velocity = this.facingDirection.scale(this.speed * this.v)
                    this.player.player?.directMessage(`当前速度为:${Math.floor((this.speed * this.v))}节`)
                    sleep(1000 / 4).then(() => {
                        if (isRun) run()
                    })
                }

                run()


                this.player.player.onRelease(() => {
                    isRun = false
                })
            }
        })
    }
}

interface IShipItem{
    ship: Ship
}

class ShipItem implements ICommodity, IShipItem {
    name: string;
    price: number;
    priceRandom: number;
    origin: string;
    probability: number;
    info: string;
    weight: number;
    ship: Ship;
    
    constructor(ship: Ship, data: ICommodity){
        this.name = data.name
        this.price = data.price
        this.priceRandom = data.priceRandom
        this.origin = data.origin
        this.probability = data.probability
        this.info = data.info
        this.weight = data.weight
        this.ship = ship
    }
}